﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wedo.Recognition.Domain;
using System.Data.Entity; 
using Wedo.Recognition.Domain.Models;
using Wedo.Domain.Models;
using Wedo.Mvc.Utility;
using Wedo.Utility.Reflection;

namespace Wedo.Recognition.Web.Models
{
    public class CreateTestData : DropCreateDatabaseIfModelChanges<EFDbContext>
    {
        private EFDbContext _dbcontext;
        protected override void Seed(EFDbContext context)
        {

            _dbcontext = context;
            CreateData();
            base.Seed(context);
        }


        private void CreateData()
        {
            try
            {
                CreateRoleUser();
                CreateBank();

                CreateSysParam();

                CreateResource();
            }
            catch (Exception e)
            {

                throw e;
            }
        }

        private void CreateSysParam()
        {
            /*
 
              */
            string[] data1 = { "欧元,EUR", "美元,USD", "人民币,CNY" };//币种

            SysParam p1 = new SysParam
            {
                Id = "001",
                ParamText = "币种",
                ParamValue = "币种"
            };

            _dbcontext.SysParams.Add(p1);
            for (int i = 0; i < data1.Length; i++)
            {
                var vals = data1[i].Split(',');
                SysParam p = new SysParam
                {
                    Id = "001" + (i + 1).ToString().PadLeft(3, '0'),

                    ParamText = vals[0],
                    ParamValue = vals[1]
                };
                _dbcontext.SysParams.Add(p);
            }
            _dbcontext.SaveChanges();

            string[] data2 = {"CAD,203053",
                            "LPG,621000",
                            "EPD,360100",
                            "CMD,180100",
                            "SDG,903099" };//部门
            SysParam p2 = new SysParam
            {
                Id = "002",
                ParamText = "部门",
                ParamValue = "部门"
            };

            _dbcontext.SysParams.Add(p2);

            for (int i = 0; i < data2.Length; i++)
            {
                var vals = data2[i].Split(',');
                SysParam p = new SysParam
                {
                    Id = "002" + (i + 1).ToString().PadLeft(3, '0'),
                    ParamText = vals[0],
                    ParamValue = vals[1]
                };
                _dbcontext.SysParams.Add(p);
            }
            _dbcontext.SaveChanges();

            string[] data3 = { "电汇,1", "汇票,2", "信用证,3", "现金,4", "支票,5", 
                                 "电汇汇入,1",  "贷记凭证提回,3", "协定存款,4", "行内转帐,5" };// 收款类型
            SysParam p3 = new SysParam
            {
                Id = "003",
                ParamText = "收款类型",
                ParamValue = "收款类型"
            };

            _dbcontext.SysParams.Add(p3);


            for (int i = 0; i < data3.Length; i++)
            {
                var vals = data3[i].Split(',');
                SysParam p = new SysParam
                {
                    Id = "003" + (i + 1).ToString().PadLeft(3, '0'),
                    ParamText = vals[0],
                    ParamValue = vals[1]
                };
                _dbcontext.SysParams.Add(p);
            }

            _dbcontext.SaveChanges();
        }

        private void CreateBank()
        {
            string[] data = { "中国银行特殊账户,10101170,USD", "汇丰银行,10101240,CNY", "三菱银行,10101220,EUR", 
                                "汇丰银行,10101250,EUR", "三菱银行,10101150,CNY","中国银行,10101480,CNY" };

            foreach (var item in data)
            {
                string[] values = item.Split(',');
                Bank bank = new Bank { BankCode = values[1], BankName = values[0], Currency = values[2] };
                _dbcontext.Bank.Add(bank);
            }
            _dbcontext.SaveChanges();
        }

        public void CreateRoleUser()
        {
            Role CSR = new Role
            {
                Id = "CSR",
                Description = "销售助理",
                RoleName = "销售助理"
            };

            {
                _dbcontext.UserInfo.Add(new UserInfo
                {
                    Name = "wedoapp\\wen.zhang",
                    StaffNameEN = "CSR_张文",
                    StaffNameCN = "CSR_张文",
                    Roles = new List<Role> { CSR },

                    Inner_Email = "Yanteng@wedoapp.com",
                    CostCenter = "CCG",
                    CompanyNo = "CTC",
                    Department = "IT",
                    Exter_Email = "Yanteng@wedoapp.com",
                    Gender = "M",
                    Password = "123456"
                });

                _dbcontext.UserInfo.Add(new UserInfo
                {
                    Name = "wedoapp\\zhenxing.chen",
                    StaffNameEN = "CSR_震星",
                    StaffNameCN = "CSR_震星",
                    Roles = new List<Role> { CSR },

                    Inner_Email = "Yanteng@wedoapp.com",
                    CostCenter = "CCG",
                    CompanyNo = "CTC",
                    Department = "IT",
                    Exter_Email = "Yanteng@wedoapp.com",
                    Gender = "M",
                    Password = "123456"
                });
            }
            _dbcontext.SaveChanges();

            Role Finace = new Role
            {
                Id = "FINACE",
                Description = "财务",
                RoleName = "财务"
            };

            _dbcontext.UserInfo.Add(new UserInfo
           {
               Name = "wedoapp\\chunyan.pan",
               StaffNameEN = "财务_春艳",
               StaffNameCN = "财务_春艳",
               Roles = new List<Role> { Finace },

               Inner_Email = "Yanteng@wedoapp.com",
               CostCenter = "CCG",
               CompanyNo = "CTC",
               Department = "IT",
               Exter_Email = "Yanteng@wedoapp.com",
               Gender = "M",
               Password = "123456"
           });

            _dbcontext.UserInfo.Add(new UserInfo
            {
                Name = "wedoapp\\wenjie.tang",
                StaffNameEN = "财务_文杰",
                StaffNameCN = "财务_文杰",
                Roles = new List<Role> { Finace },

                Inner_Email = "Yanteng@wedoapp.com",
                CostCenter = "CCG",
                CompanyNo = "CTC",
                Department = "IT",
                Exter_Email = "Yanteng@wedoapp.com",
                Gender = "M",
                Password = "123456"
            });
            _dbcontext.SaveChanges();



            Role Admin = new Role
            {
                Id = "ADMIN",
                Description = "财务",
                RoleName = "财务"
            };

            _dbcontext.UserInfo.Add(new UserInfo
            {
                Name = "SANMEN-PC\\sanamen",
                StaffNameEN = "管理员",
                StaffNameCN = "管理员",
                Roles = new List<Role> { Admin },

                Inner_Email = "Yanteng@wedoapp.com",
                CostCenter = "CCG",
                CompanyNo = "CTC",
                Department = "IT",
                Exter_Email = "Yanteng@wedoapp.com",
                Gender = "M",
                Password = "123456"
            });
            _dbcontext.SaveChanges();
        }

        public void CreateResource()
        {
            var role = _dbcontext.Role.Where(m => m.Id == "ADMIN").ToList();
            Module sys = new Module { CreatedBy = "System", ModifiedBy = "System", Name = "系统管理" };
            _dbcontext.Resource.Add(
                new Resource { Navigator = true, Action = "Index",
                               Controller = "Bank",
                               Module = sys,
                               Name = "银行管理",
                               Roles = role
                });

            _dbcontext.Resource.Add(
                new Resource { Navigator = true, Action = "Index",
                               Controller = "UserInfo",
                               Module = sys,
                               Name = "用户管理",
                               Roles = role
                });

            _dbcontext.Resource.Add(
                new Resource { Navigator = true, Action = "Index",
                               Controller = "ParamList",
                               Module = sys,
                               Name = "参数管理",
                               Roles = role
                });

            _dbcontext.SaveChanges();
        }
    }

}